package com.example.yichuangyun.mvvm.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.yichuangyun.R;
import com.example.yichuangyun.databinding.ItemMainPersonInfoBinding;
import com.example.yichuangyun.model.company.GsBaseInfoModel;
import com.example.yichuangyun.utils.CommonUtil;

import java.util.List;

/**
 * 主要人员
 */
public class MainPersonInfoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final Context context;
    private final List<GsBaseInfoModel.MainPersonModel> data;

    public MainPersonInfoAdapter(Context context, List<GsBaseInfoModel.MainPersonModel> data) {
        this.context = context;
        this.data = data;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemMainPersonInfoBinding binding = ItemMainPersonInfoBinding.inflate(LayoutInflater.from(context), parent, false);
        return new MyViewHolder(binding);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            MyViewHolder myViewHolder = (MyViewHolder) holder;
            GsBaseInfoModel.MainPersonModel model = data.get(position);
            if (position % 3 == 0) {
                myViewHolder.binding.tvSurname.setBackgroundResource(R.drawable.shape_60bdbc_2dp_bg);
            } else if (position % 3 == 1) {
                myViewHolder.binding.tvSurname.setBackgroundResource(R.drawable.shape_e7bac9_2dp_bg);
            }
            if (position % 3 == 2) {
                myViewHolder.binding.tvSurname.setBackgroundResource(R.drawable.shape_cbd194_2dp_bg);
            }
            if (!TextUtils.isEmpty(model.getName())) {
                myViewHolder.binding.tvSurname.setText(model.getName().substring(0, 1));
                myViewHolder.binding.tvName.setText(model.getName());
            }
            myViewHolder.binding.tvJobPosition.setText(CommonUtil.setText(model.getJobTitle()));
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        ItemMainPersonInfoBinding binding;

        public MyViewHolder(ItemMainPersonInfoBinding itemMainPersonInfoBinding) {
            super(itemMainPersonInfoBinding.getRoot());
            binding = itemMainPersonInfoBinding;
        }
    }
}
